import json

from flask import Flask
from flask import render_template, jsonify
from flask import request

from sqlconn import remote_database

import config

app = Flask(__name__)
app.secret_key = config.SECRET_KEY


@app.route('/')
@app.route('/welcome')
def index(user_id=None):
    return render_template('./index.html')


@app.route('/submit',methods=['GET','POST'])
def submit():
    info = request.args
    print(info)
    username = ''
    answer = json.loads(info['answer'])
    id = ''
    result = info['type']
    print(answer)
    print(result)
    sql = f"INSERT INTO info (username, id, q1, q2, q3, q4, q5, q6, q7, q8, q9, q10, q11, result) values (\"{username}\", \"{id}\", \"{answer[0]}\",\"{answer[1]}\",\"{answer[2]}\",\"{answer[3]}\",\"{answer[4]}\",\"{answer[5]}\",\"{answer[6]}\",\"{answer[7]}\",\"{answer[8]}\",\"{answer[9]}\",\"{answer[10]}\",\"{result}\")"
    print(sql)
    if remote_database.insert(sql):
        response = {
            'success': True
        }
        return jsonify(response)
    else:
        response = {
            'success': False,
        }
        return jsonify(response)


@app.route('/test',methods=['GET'])
def test():
    print('前后端连接成功！')
    response = {
        "success": True,
        "error_code": 0
    }
    return jsonify(response)

if __name__ == '__main__':
    app.run(debug=True)
